package com.fsai.cms.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 *
 * @project: pcdc-web
 *
 * @author: JaffeHe
 * @email: JaffeHe@foxmail.com
 * @date: 2020-06-08 10:35
 *
 * @ChangeLog<br/>
 * 				Developer Date Comment <br/>
 */
@Configuration
public class WebAppConfig extends WebMvcConfigurationSupport {

	@Autowired
	SwaggerInterceptor swaggerInterceptor;
	@Autowired
	AuthInterceptor authInterceptor;

	@Value("${cms.upload.file}")
	private String uploadPath;

	@Override
	public void addFormatters(FormatterRegistry registry) {
		registry.addConverterFactory(new BaseEnumConverterFactory());
	}

	@Override
	protected void addInterceptors(InterceptorRegistry registry) {
		/**
		 * 拦截器按照顺序执行,如果不同拦截器拦截存在相同的URL，前面的拦截器会执行，后面的拦截器将不执行
		 */
		//1-swagger拦截器
		InterceptorRegistration swaRegistration = registry.addInterceptor(swaggerInterceptor);
		swaRegistration.addPathPatterns("/swagger-ui.html", "/doc.html");//拦截路径
		//auth授权拦截器
		InterceptorRegistration authRegistration = registry.addInterceptor(authInterceptor);
		//拦截路径
		authRegistration.addPathPatterns("/admin/**");
		//排除登录、退出
		authRegistration.excludePathPatterns("/admin/login*", "/admin/logout");
	}

	/**
	 * 重新指定静态资源
	 *
	 * @param registry
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// 解决 SWAGGER2 404报错
		registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
		//添加服务器图片映射资源 - 后缀/
		//参考文章：https://blog.csdn.net/zsl129/article/details/52906762
		registry.addResourceHandler("/profile/**").addResourceLocations("file:" + this.uploadPath);
		//将所有/static/** 访问都映射到classpath:/static/ 目录下
		registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");

		super.addResourceHandlers(registry);
	}

	@Override
	protected void addViewControllers(ViewControllerRegistry registry) {
		//		registry.addViewController("/").setViewName("redirect:/biz/indexo");//默认主页
		//		registry.addViewController("/biz/").setViewName("redirect:/biz/indexo");//默认主页
		//		registry.addViewController("/admin/").setViewName("redirect:/admin/index");//Admin默认主页
		registry.setOrder(Ordered.HIGHEST_PRECEDENCE);

		super.addViewControllers(registry);
	}

}
